﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleListDto, RoleServiceProxy, ContactDto, ContactServiceProxy, TemplateServiceProxy, WorkStationDto, WorkStationUpdateOrOutput } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './work-station.component.html',
    styleUrls: ['./work-station.component.less'],
    animations: [appModuleAnimation()]
})
export class WorkStationComponent extends AppComponentBase implements OnInit  {

    @ViewChild('dt') dataTable: Table;

    displayDialog: boolean;

    station: WorkStationUpdateOrOutput;    
    selectedStation: WorkStationUpdateOrOutput;    
    newStation: boolean;    
    stations: WorkStationUpdateOrOutput[];

    constructor(
        injector: Injector,
        private _templateServiceProxy: TemplateServiceProxy
        ) 
        {
            super(injector);
            
        }

    
    
    cols: any[];
    ngOnInit(): void {
            this._templateServiceProxy.getAllStations()
            .subscribe(result => {
                this.stations = result;
              });

            this.cols = [
                { field: 'id', header: '编号' },
                { field: 'stationName', header: '工位名称' }
            ];
    }

    showDialogToAdd() {
        this.newStation = true;
        this.station = new WorkStationUpdateOrOutput();
        this.displayDialog = true;
    }


    save() {        

      /*  this._templateServiceProxy.createWorkStation(this.station)
        .subscribe(result => {
                              
            if (this.newStation){                

                let newstation  = new WorkStationUpdateOrOutput();
                newstation.stationName = this.station.stationName;
                newstation.id = result;
                this.stations.push(newstation);   
            } 
            else
                this.stations[this.stations.indexOf(this.selectedStation)] = this.station;

            this.station = null;
            this.displayDialog = false;
          });
          */

         if(this.newStation){    
        
            this._templateServiceProxy.createWorkStation(this.station)
            .subscribe(result => {
             
                this.stations.push(this.station);   
            });
        }else
        {
            this._templateServiceProxy.updateWorkStation(this.station)
            .subscribe(() => {
              
                this.stations[this.stations.indexOf(this.selectedStation)] = this.station;
            });

        }

       
        this.displayDialog = false;   
        
    }

    delete() {
        let index = this.stations.indexOf(this.selectedStation);
        this._templateServiceProxy.deleteWorkStation(this.station.id)
        .subscribe(result => {
            this.stations = this.stations.filter((val, i) => i != index);
          });
        
        this.station = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newStation = false;
        this.station = this.cloneStation(event.data);
        this.displayDialog = true;
    }

    cloneStation(c: WorkStationUpdateOrOutput): WorkStationUpdateOrOutput {
        let station = new WorkStationUpdateOrOutput();
        for (let prop in c) {
            station[prop] = c[prop];
        }
        return station;
    }

      
    print(qrcode:string,stationName:string) {
        this._templateServiceProxy.printStationQr(qrcode,stationName).subscribe(()=>{
        this.notify.info('打印成功！');
        });
    }
}
